//index.js
//获取应用实例
const app = getApp()
var api = require('../../utils/api.js')
var util = require('../../utils/util.js');

Page({
  data: {
    indicatorDots: false,
    autoplay: false,
    interval: 5000,
    duration: 500,
    circular: true,
    lastTime: 0
  },

  onLoad: function () {
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      windowHeight: app.globalData.windowHeight,
    })
    this.authorization();
  },
  onShow:function(){
    this.setData({
      lastTime: 0
    })
  },

  bannerList: function () {
    var params = new Object();
    params.type = 2;
    var that = this;
    api.POST({
      params: params,
      url: app.globalData.base_url + 'user/banner',
      success: function (res) {
        wx.hideLoading()
        if (util.isEmpty(res.data.code)) {
          if (res.data.data.lenght > 1) {
            var indicatorDots = true
          } else {
            var indicatorDots = false
          }
          that.setData({
            bannerList: res.data.data,
            indicatorDots: indicatorDots
          })
        }
      },
      fail: function (fail) {
        wx.hideLoading()
        util.showError("登录失败，请检查网络");

      }
    })
  },

  sweepCode: function () {
    wx.scanCode({
      onlyFromCamera: true,
      success: (res) => {
        var scanCode = res.result;
        // wx.navigateTo({
        //   url: '../orderCode/orderCode?oid=4463094' ,
        // })
        if (scanCode.lastIndexOf("type=px") >= 0) {
          var shopId = scanCode.substring(scanCode.lastIndexOf("=") + 1);
          wx.navigateTo({
            url: '../classification/classification?index=2&shopId=' + shopId,
          })
        } else if (scanCode.lastIndexOf("type=oid") >= 0) {
          var oid = scanCode.substring(scanCode.lastIndexOf("=") + 1);
          wx.navigateTo({
            url: '../orderCode/orderCode?oid=' + oid,
          })
        } else {
          wx.navigateTo({
            url: '../pickup/pickup'
          })
        }
      },
      fail: function (e) {
        if (e.errMsg != 'scanCode:fail cancel') {
          util.showError("扫码失败");
        }
      }
    });
  },

  // 登录
  authorization: function () {
    var that = this
    wx.login({
      success: function (res_login) {
        wx.getUserInfo({
          withCredentials: true,
          success: function (res_user) {
            var login_code = res_login.code;
            var login_encryptedData = res_user.encryptedData;
            var login_iv = res_user.iv;
            that.autoLogin(login_code, login_encryptedData, login_iv);
            that.setData({
              avatarUrl: res_user.userInfo.avatarUrl,
              nickName: res_user.userInfo.nickName,
            });
          }, fail: function (fail_res) {
            if (fail_res.errMsg == "getUserInfo:fail auth deny") {
              that.showModal();
            } else {
              util.showError("登录失败");
            }
          }
        })
      }, fail: function (fail_login) {
        //console.log("fail");
      }
    })
  },

  autoLogin: function (login_code, login_encryptedData, login_iv) {
    var that = this
    var params = {
      code: login_code,
      encryptedData: login_encryptedData,
      iv: login_iv
    }
    api.POST({
      params: params,
      url: app.globalData.login_url,
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          app.globalData.accountInfo = res.data.data.result;
          that.bannerList();
        }
      },
      fail: function (fail) {
        util.showError("登录失败，请检查网络");
      }
    })
  },

  // 重新授权
  reAuthorization: function () {
    var that = this
    wx.openSetting({
      success: function (res) {
        if (res.authSetting["scope.userInfo"] && res.authSetting["scope.userLocation"]) {
          //检测是否有登录信息
          if (that.data.nickName == "" && that.data.avatarUrl == "") {
            that.authorization();
          }
          //检测是否有定位信息
          if (that.data.longitude == 0 && that.data.latitude == 0) {
            that.adjustLocation();
          }
        } else {
          that.showModal();
        }
      }
    })
  },

  showModal: function () {
    var that = this;
    wx.showModal({
      title: '警告',
      content: '若不授权，则无法使用',
      confirmText: '授权',
      cancelText: '不授权',
      success: function (suress) {
        if (suress.confirm) {
          that.reAuthorization();
        } else {
          that.showModal();
        }
      }
    })
  },
  shopList:function(e){
    var curTime = e.timeStamp;
    var lastTime = this.data.lastTime;
    this.setData({
      lastTime: curTime
    })
    if (lastTime > 0) {
      if (curTime - lastTime < 500) {
        return false;
      }
    }
    wx.navigateTo({
      url: '../shopList/shopList'
    })
  },
  shopping: function (e) {
    var curTime = e.timeStamp;
    var lastTime = this.data.lastTime;
    this.setData({
      lastTime: curTime
    })
    if (lastTime > 0) {
      if (curTime - lastTime < 500) {
        return false;
      }
    }
    wx.navigateTo({
      url: '../shopping/shopping'
    })
  },
  activity: function (e) {
    var curTime = e.timeStamp;
    var lastTime = this.data.lastTime;
    this.setData({
      lastTime: curTime
    })
    if (lastTime > 0) {
      if (curTime - lastTime < 500) {
        return false;
      }
    }
    wx.navigateTo({
      url: '../activity/activity'
    })
  }

})
